package AddPresent;


import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import AddProduct.HTML;
import AddProduct.SQL;
import AddProduct.SQLStatements;

/**
 * Servlet implementation class InsertIntoItemPresent
 * This servlet insert data into ITEM_PRESENTS table in database.
 * @author desso
 */
@WebServlet("/InsertIntoItemPresent")
public class InsertIntoItemPresent extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertIntoItemPresent() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 HttpSession sess = request.getSession();	
			
		    try {	   
		    	Connection databaseConnection = SQL.getConnection();		
		    	insertIntoItemPresents(request, sess, databaseConnection);
				HTML.PrintASuccessMessage(response, "present");			
			} catch (SQLException | ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

		private void insertIntoItemPresents(HttpServletRequest request, HttpSession sess, Connection dbConnection) throws SQLException {
			PreparedStatement prepareStatement = dbConnection.prepareStatement(SQLStatements.INSERT_INTO_ITEM_PRESENTS_TABLE);
			prepareStatement.setString(1, (String) request.getAttribute("item"));
			prepareStatement.setInt(2, (int) sess.getAttribute("present_id"));
			prepareStatement.executeUpdate();
		}
}
